// 
// SoapTrace.Net - A soap message monitoring utility. 
// 
// Copyright (C) 2004 Matthew Ward
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
// Matthew Ward (mrward@users.sourceforge.net)
// 

using NUnit.Framework;
using System;
using System.Text;
using WinPcap;
using WinPcap.Tests.Utils;

namespace WinPcap.Tests
{
	/// <summary>
	/// Tests the Tcp Packet class.
	/// </summary>
	[ TestFixture ]
	public class TcpPacketTestFixture
	{
		/// <summary>
		/// Tests a Tcp SYN packet.
		/// </summary>
		[ Test ]
		public void TcpSynPacket( )
		{
			string Data = 
				"0000  45 00 00 30 45 C0 40 00  80 06 23 02 C8 01 01 01 \n" +
				"0010  C8 01 01 02 04 4A 1B 5A  D0 22 05 0A 00 00 00 00 \n" +
				"0020  70 02 FF FF FC 48 00 00  02 04 05 B4 01 01 04 02 ";

			byte[ ] Bytes = TestUtility.GetBytesFromString( Data ); 

			IPPacket IPPacket = new IPPacket( Bytes );

			TcpPacket Packet = ( TcpPacket )IPPacket.InnerPacket;

			// Source IP address.

			Assert.AreEqual( "200.1.1.1:1098", 
				Packet.SourceAddress.ToString( ),
				"Source IP address mismatch." );

			// Destination IP address.

			Assert.AreEqual( "200.1.1.2:7002", 
				Packet.DestinationAddress.ToString( ),
				"Destination IP address mismatch." );

			// Sequence number.

			Assert.AreEqual( 3491890442,
				Packet.SequenceNumber,
				"Sequence number mismatch." );

			// Acknowledgement number.

			Assert.AreEqual( 0, 
				Packet.AcknowledgementNumber, 
				"Ack number mismatch." );

			// Is it a SYN?

			Assert.AreEqual( TcpControlTypes.Syn,
				( TcpControlTypes )Packet.ControlFlag,
				"Should be a SYN packet." );

			// Should not be any data held after the TCP header.

			Assert.AreEqual( 0,
				Packet.GetDataBytes( ).Length,
				"Expected no data inside the TCP packet." );
		}

		/// <summary>
		/// Tests parsing a tcp packet that contains some application defined
		/// data.
		/// </summary>
		[ Test ]
		public void TcpPacketWithSentMessageData( )
		{
			string Data = 
				"0000  45 00 00 2D 9A C0 40 00  80 06 CE 04 C8 01 01 01 \n" +
				"0010  C8 01 01 02 04 46 1B 5A  B6 52 78 E1 D4 98 3E CD \n" +
				"0020  50 18 44 70 53 45 00 00  48 65 6C\n" +
				"0030  6C 6F ";

			byte[ ] Bytes = TestUtility.GetBytesFromString( Data ); 

			IPPacket IPPacket = new IPPacket( Bytes );

			TcpPacket Packet = ( TcpPacket )IPPacket.InnerPacket;

			// Source IP address.

			Assert.AreEqual( "200.1.1.1:1094", 
				Packet.SourceAddress.ToString( ),
				"Source IP address mismatch." );

			// Destination IP address.

			Assert.AreEqual( "200.1.1.2:7002", 
				Packet.DestinationAddress.ToString( ),
				"Source IP address mismatch." );

			// Is it a PSH, ACK?

			Assert.AreEqual( TcpControlTypes.Psh | TcpControlTypes.Ack, 
				( TcpControlTypes )Packet.ControlFlag,
				"Should be a PSH, ACK packet." );

			// Should not be some data held after the TCP header.

			Assert.AreEqual( 5,
				Packet.GetDataBytes( ).Length,
				"Expected some data inside the TCP packet." );

			// Check data contents.
			Assert.AreEqual( "Hello",
				UnicodeEncoding.UTF8.GetString( Packet.GetDataBytes( ) ),
				"Data contents are incorrect." );

		}
	}
}

